package com.springdemo.spring5.TestSpringDemo;

import com.springdemo.spring5.AOP.aopanno.User;
import com.springdemo.spring5.FactoryBean.MyBean;
import com.springdemo.spring5.Orders;
import com.springdemo.spring5.autowrite.Emp;
import com.springdemo.spring5.bean2.Orders2;
import com.springdemo.spring5.collectionType.Book;
import com.springdemo.spring5.collectionType.Course;
import com.springdemo.spring5.collectionType.Stu;
import com.springdemo.spring5.config.SpringConfig;
import com.springdemo.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created with IntelliJ IDEA.
 * User: zjz
 * Date: 2022/02/23 20:03
 * Description:
 * Version: V1.0
 */
public class TestDemo1 {
    @Test
    public void testCollection1(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean5.xml");
        Stu stu=context.getBean("stu",Stu.class);//xml文件的id
        stu.test();

    }
    @Test
    public void testCollection2(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean6.xml");
        Book book1=context.getBean("book", Book.class);//xml文件的id
        Book book2=context.getBean("book", Book.class);//xml文件的id
        //book.test();
        System.out.println(book1);
        System.out.println(book2);

    }
    @Test
    public void testFactoryBean(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean7.xml");
        Course course=context.getBean("myBean", Course.class);//xml文件的id
        System.out.println(course);
    }

    @Test
    public void testLifeCycle(){
        //ApplicationContext context=new ClassPathXmlApplicationContext("bean8.xml")
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("bean8.xml");
        Orders2 orders2=context.getBean("orders2", Orders2.class);
        System.out.println("第四步，获取创建bean实例对象"+"   "+orders2);
        //手动让bean实例销毁
         context.close();
    }

    @Test
    public void testAutoWrite(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean9.xml");
        Emp emp=context.getBean("emp",Emp.class);
        System.out.println(emp);
    }

    @Test
    public void testScan(){
        ApplicationContext context=new ClassPathXmlApplicationContext("scan.xml");
        //第一个参数值，跟注解中的value值一样
        UserService userService=context.getBean("userService",UserService.class);
        System.out.println(userService);
        userService.add();
    }
    @Test
    public void testAllConfig(){
        //加载配置类
        ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
        //第一个参数值，跟注解中的value值一样
        UserService userService=context.getBean("userService",UserService.class);
        System.out.println(userService);
        userService.add();
    }
    @Test
    public void testAOPAnno(){
        ApplicationContext context=new ClassPathXmlApplicationContext("aspectJ.xml");
        User user=context.getBean("user",User.class);
        user.add();
    }
    @Test
    public void testAopXml(){
        ApplicationContext context=new ClassPathXmlApplicationContext("aopxml.xml");
        com.springdemo.spring5.AOP.aopxml.Book book=context.getBean("bookxml", com.springdemo.spring5.AOP.aopxml.Book.class);
        book.buy();

    }

}
